package com.hhkj.admin.conf.common.validator.commission;

import com.hhkj.admin.common.validator.AbstractValidatorChain;
import com.hhkj.admin.conf.model.extend.CommissionRate;
import com.hhkj.admin.conf.model.req.CommissionRateReq;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;

@Component
@Order(1)
public class RateValidator extends AbstractCommissionRateValidatorChain{
    @Override
    protected boolean doValidate(CommissionRateReq bean, ConstraintValidatorContext constraintValidatorContext, AbstractValidatorChain validatorChain) {
        BigDecimal upOneLevelRate = bean.getUpOneLevelRate();
        BigDecimal thisLevelRate = bean.getThisLevelRate();
        if (upOneLevelRate.compareTo(new BigDecimal("0.00")) < 0 || upOneLevelRate.compareTo(new BigDecimal("100.00")) > 0) {
            constraintValidatorContext.buildConstraintViolationWithTemplate("上级返利比例不合法")
                    .addPropertyNode("upOneLevelRate")
                    .addConstraintViolation();
            return false;
        }
        if (thisLevelRate.compareTo(new BigDecimal("0.00")) < 0 || thisLevelRate.compareTo(new BigDecimal("100.00")) > 0) {
            constraintValidatorContext.buildConstraintViolationWithTemplate("本级返利比例不合法")
                    .addPropertyNode("thisLevelRate")
                    .addConstraintViolation();
            return false;
        }
        return validatorChain.validateNext(bean, constraintValidatorContext);
    }
}
